{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## PySnooper\n",
    "\n",
    "Github：<https://github.com/lotapp/PySnooper>\n",
    "\n",
    "`pip install pysnooper`\n",
    "\n",
    "使用：**分析整个代码**\n",
    "```py\n",
    "@pysnooper.snoop()\n",
    "def avg(args):\n",
    "    return sum(args) / len(args)\n",
    "```\n",
    "\n",
    "PS：调试代码片段\n",
    "```py\n",
    "with pysnooper.snoop():\n",
    "    code\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import functools\n",
    "import pysnooper\n",
    "\n",
    "# 分析整个代码\n",
    "@pysnooper.snoop()\n",
    "def sum(args):\n",
    "    return functools.reduce(lambda x, y: x + y, args)\n",
    "\n",
    "def avg(args):\n",
    "    # return sum(args) / len(args)\n",
    "    sum_num = sum(args)\n",
    "    # 分析片段代码\n",
    "    with pysnooper.snoop():\n",
    "        avg_num = sum_num / len(args)\n",
    "    return avg_num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Starting var:.. args = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ... 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]\n",
      "12:21:19.846556 call         6 def sum(args):\n",
      "12:21:19.847553 line         7     return functools.reduce(lambda x, y: x + y, args)\n",
      "12:21:19.847553 return       7     return functools.reduce(lambda x, y: x + y, args)\n",
      "Return value:.. 5050\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "5050"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求和\n",
    "sum(list(range(1, 101)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Starting var:.. args = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ... 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]\n",
      "12:21:19.940523 call         6 def sum(args):\n",
      "12:21:19.941526 line         7     return functools.reduce(lambda x, y: x + y, args)\n",
      "12:21:19.941526 return       7     return functools.reduce(lambda x, y: x + y, args)\n",
      "Return value:.. 5050\n",
      "New var:....... args = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ... 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]\n",
      "New var:....... sum_num = 5050\n",
      "12:21:19.942524 line        14         avg_num = sum_num / len(args)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "50.5"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 平均值\n",
    "avg(list(range(1, 101)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## other"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
